package com.jiazhong.example.project.shoppingcart.mapper;

import com.jiazhong.example.project.shoppingcart.bean.Carts;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CartsMapper {
    @Insert("insert into carts values(null,#{bookId},#{userId},1)")
    int save(Carts carts);

    @Update("update carts set number=number+1 where user_id=#{userId} and book_id=#{bookId}")
    int update(Carts carts);

    @Update("update carts set number=number-1 where user_id=#{userId} and book_id=#{bookId}")
    int update1(Carts carts);

    @Update("update carts set number=number+${quantity} where user_id=#{userId} and book_id=#{bookId}")
    int update2(Carts carts);

    @Select("select c.*,b.*,book_id as 'bookId',user_id as 'userId' from carts c left join books b on c.book_id=b.id where user_id=#{userId}")
    List<Carts> findByUserId(Integer userId);

    @Delete("delete from carts where id=#{id}")
    int deleteById(int id);

    @Select("select c.*,book_id as 'bookId' from carts c where id=#{id}")
    Carts findById(int id);

    @Delete("<script>delete from carts where id in <foreach collection='ids' item='e' open='(' close=')' separator=','>#{e}</foreach></script>")
    int deleteByBatch(@Param("ids") List<String> ids);

    @Select("<script>select c.*,book_id as 'bookId',user_id as 'userId' from carts c where id in <foreach collection='ids' item='e' open='(' close=')' separator=','>#{e}</foreach></script>")
    List<Carts> findByIds(@Param("ids") List<String> ids);

    @Delete("delete from carts where user_id=#{userId}")
    int deleteByUserId(int userId);

}
